package org.graduation.recruitment.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.graduation.recruitment.entity.dto.UserLoginDTO;
import org.graduation.recruitment.entity.dto.UserRegisterDTO;
import org.graduation.recruitment.entity.exception.ServiceException;
import org.graduation.recruitment.entity.pojo.StudentUser;
import org.graduation.recruitment.entity.pojo.User;
import org.graduation.recruitment.entity.pojo.resume.Resume;
import org.graduation.recruitment.mapper.StudentUserMapper;
import org.graduation.recruitment.mapper.resume.ResumeMapper;
import org.graduation.recruitment.service.StudentUserService;
import org.graduation.recruitment.utils.Encrypt;
import org.graduation.recruitment.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentUserServiceImpl extends ServiceImpl<StudentUserMapper, StudentUser> implements StudentUserService {
    @Autowired
    private Token token;
    @Autowired
    private Encrypt encrypt;
    @Autowired
    private StudentUserMapper studentUserMapper;
    @Autowired
    private ResumeMapper resumeMapper;

    @Override
    public String login(UserLoginDTO user) {
        user.setPassword(encrypt.md5_32(user.getPassword()));
        StudentUser studentUser = studentUserMapper.login(user);
        if (studentUser != null) {
            return token.generateToken(studentUser);
        } else {
            return null;
        }
    }

    @Override
    public boolean register(UserRegisterDTO user) {
        LambdaQueryWrapper<StudentUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentUser::getStudentId, user.getAccount());
        if(studentUserMapper.exists(queryWrapper)){
            return false;
        }
        StudentUser studentUser = new StudentUser();
        studentUser.setStudentId(user.getAccount());
        studentUser.setPassword(encrypt.md5_32(user.getPassword()));
        boolean result = studentUserMapper.insert(studentUser) > 0;
        if (result) {
            Resume resume = new Resume();
            resume.setStudentId(studentUser.getId());
            resumeMapper.insert(resume);
        }
        return result;
    }

    @Override
    public User getUserInfoByToken(String tokenStr) {
        Long id = token.getUidByToken(tokenStr);
        return getUserInfoById(id);
    }

    @Override
    public User getUserInfoById(Long id) {
        return studentUserMapper.selectById(id);
    }

    @Override
    public boolean updatePassword(Long id, String oldPassword, String newPassword) {
        String oldPasswordMd5 = encrypt.md5_32(oldPassword);
        StudentUser studentUser = studentUserMapper.selectOne(idAndPasswordQueryWrapper(id, oldPasswordMd5));
        if (studentUser == null) {
            throw new ServiceException("账号原密码错误");
        } else {
            return studentUserMapper.update(updatePasswordWrapper(id, encrypt.md5_32(newPassword))) == 1;
        }
    }

    @Override
    public boolean updateInfo(User user) {
        return studentUserMapper.update(updateInfoWrapper((StudentUser) user)) == 1;
    }

    private LambdaQueryWrapper<StudentUser> idAndPasswordQueryWrapper(Long id, String passwordMD5) {
        LambdaQueryWrapper<StudentUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StudentUser::getId, id);
        lambdaQueryWrapper.eq(StudentUser::getPassword, passwordMD5);
        return lambdaQueryWrapper;
    }

    private LambdaUpdateWrapper<StudentUser> updatePasswordWrapper(Long id, String passwordMD5) {
        LambdaUpdateWrapper<StudentUser> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StudentUser::getId, id);
        lambdaUpdateWrapper.set(StudentUser::getPassword, passwordMD5);
        return lambdaUpdateWrapper;
    }

    private LambdaUpdateWrapper<StudentUser> updateInfoWrapper(StudentUser user) {
        LambdaUpdateWrapper<StudentUser> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StudentUser::getId, user.getId());
        lambdaUpdateWrapper.set(StudentUser::getName, user.getName());
        lambdaUpdateWrapper.set(StudentUser::getAddress, user.getAddress());
        lambdaUpdateWrapper.set(StudentUser::getEmail, user.getEmail());
        lambdaUpdateWrapper.set(StudentUser::getPhone, user.getPhone());
        lambdaUpdateWrapper.set(StudentUser::getSex, user.getSex());
        lambdaUpdateWrapper.set(StudentUser::getBirthDay, user.getBirthDay());
        lambdaUpdateWrapper.set(StudentUser::getSchool, user.getSchool());
        return lambdaUpdateWrapper;
    }
}
